package developer.排序算法.选择排序;

import java.util.Arrays;

/**
 * @author zhangyongkang
 * @time 2025/7/18 14:12
 * @description
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] arr = {99, 55, 2, 3, 9, 10, 22, 34, 67, 89, 69, 92, 101, 102};
        selectSort(arr);
        Arrays.stream(arr).forEach(a -> {
            System.out.println(a);
            System.out.println("--");
        });
    }

    public static void selectSort(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            //选择初始点
            int maxIdx = i;
            for (int j = i; j < length; j++) {
                if (arr[j] < arr[maxIdx]) {
                    maxIdx = j;
                }
            }
            if (maxIdx != i) {
                //交换
                int temp = arr[i];
                arr[i] = arr[maxIdx];
                arr[maxIdx] = temp;
            }
        }
    }
}
